package fr.mistra.exercice.bibliotheque.presentation;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fr.mistra.exercice.bibliotheque.dao.DAOException;
import fr.mistra.exercice.bibliotheque.dao.NoSuchIdException;
import fr.mistra.exercice.bibliotheque.modeles.Bibliotheque;
import fr.mistra.exercice.bibliotheque.modeles.Livre;
import fr.mistra.exercice.bibliotheque.services.BibliothequeService;
import fr.mistra.exercice.bibliotheque.services.LivreService;
import fr.mistra.exercice.bibliotheque.services.NoMoreRoomException;

/**
 * Servlet implementation class AjouterLivreServlet
 */
public class AjouterLivreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjouterLivreServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long id;
		try {
			id = Long.valueOf(request.getParameter("idBiblio")).longValue();
			request.getRequestDispatcher("/JSP/AjouterLivre.jsp").forward(request, response);
		}
		catch (NumberFormatException e) {
			request.getRequestDispatcher("/JSP/ErreurGenerique.jsp").forward(request, response);			
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		long idBiblio = Long.valueOf(request.getParameter("idBiblio")).longValue();
		try {
			BibliothequeService service = BibliothequeService.getService();
			Bibliotheque b = service.getBibliotheque(idBiblio);
			LivreService livreService = LivreService.getLivreService();
			String titre = request.getParameter("titre");
			String auteur = request.getParameter("auteur");
			String isbn = request.getParameter("isbn");
			Livre l = livreService.creerNouveauLivre(titre, auteur, isbn);
			System.out.println("Creation livre ! (id = " + l.getId() + ")");
			service.ajouterLivre(b, l);
			System.out.println("Le livre a obtenu l'ID " + l.getId());
			request.getRequestDispatcher("VoirBibliothequeServlet?id=" + idBiblio + "&resultat="+l.getId()).forward(request,  response);
		}
		catch (DAOException e) {
			e.printStackTrace();
			request.getRequestDispatcher("/JSP/ErreurDAO.jsp").forward(request, response);
		}
		catch (NoSuchIdException e) {
			e.printStackTrace();
			System.out.println("La bibliotheque n�" + idBiblio + " n'existe pas.");
			request.getRequestDispatcher("/JSP/ErreurDAO.jsp").forward(request, response);
		}
		catch (NoMoreRoomException e) {
			e.printStackTrace();
			System.out.println("La biblioth�que est pleine !");
			request.getRequestDispatcher("VoirBibliothequeServlet?id=" + idBiblio + "&resultat=plein)");
		}
	}

}
